package domain.stack;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 
 * @author igordv
 *
 * @param <E>
 */

@SuppressWarnings("unchecked")
public class IlNode <E> extends AtomicReference{

	private static final long serialVersionUID = -7053202920931753202L;

	public final E item;
	public IlNode<E> next;
	public AtomicReference<Status> nodesStatus = new AtomicReference<Status>(Status.STBL);
	
	public IlNode(E item) {
		this.item = item;
	}
	
	@Override
	public String toString() {
		
		return item.toString();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result /*+ ((next == null) ? 0 : next.hashCode())*/;
		result = prime * result	+ ((nodesStatus == null) ? 0 : nodesStatus.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof IlNode))
			return false;
		final IlNode other = (IlNode) obj;
		if (this.next == null) {
			if (other.next != null)
				return false;
		} else if (!(this.next==other.next)) // !next.equals(other.next)
			return false;
		if (this.nodesStatus == null) {
			if (other.nodesStatus != null)
				return false;
		} else if (!this.nodesStatus.get().equals(other.nodesStatus.get())) //nodesStatus.equals(other.nodesStatus)
			return false;
		return true;
	}
	
	public static void main(String[] args) {
		IlNode<Integer> n1 = new IlNode<Integer>(1);
		IlNode<Integer> n2 = new IlNode<Integer>(2);
		
		
		System.out.println(n1==n2);		   //false
		System.out.println(n1.equals(n2)); //true
		
		IlNode<Integer> n3 = new IlNode<Integer>(3);
		n2.next = n3;
		System.out.println(n1.equals(n2)); //false
		n1.next = n3;
		System.out.println(n1.equals(n2)); // true
		n1.nodesStatus = new  AtomicReference<Status>(Status.UNSTBL);
		System.out.println(n1.equals(n2)); //false
		n2.nodesStatus = new  AtomicReference<Status>(Status.UNSTBL);
		System.out.println(n1.equals(n2)); //true
	}
}